﻿Imports System.Text
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class FrmPresupposition
    Dim JsonPre As JObject

    Private Sub FrmPresupposition_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Icon = FrmMain.Icon

        Dim f As String
        Dim i As Integer
        ListView1.Items.Clear()
        ListView2.Items.Clear()
        ListView3.Items.Clear()
        f = Encoding.GetEncoding("utf-8").GetString(My.Resources.command)
        JsonPre = CType(JsonConvert.DeserializeObject(f), JObject)
        For i = 0 To JsonPre("app").Count - 1
            Dim Item As ListViewItem
            Item = ListView1.Items.Add(JsonPre("app")(i)("name").ToString)
            Item.SubItems.Add(JsonPre("app")(i)("info").ToString)
            If ItemExists(JsonPre("app")(i)("command").ToString) Then
                Item.Checked = True
            End If
        Next
        For i = 0 To JsonPre("sys").Count - 1
            Dim Item As ListViewItem
            Item = ListView2.Items.Add(JsonPre("sys")(i)("name").ToString)
            Item.SubItems.Add(JsonPre("sys")(i)("info").ToString)
            If ItemExists(JsonPre("sys")(i)("command").ToString) Then
                Item.Checked = True
            End If
        Next
    End Sub

    Private Sub FrmPresupposition_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        LblInfo.Location = New Point(10, 10)
        TabControl.Location = New Point(10, LblInfo.Bottom + 5)
        TabControl.Size = New Size(ClientSize.Width - 20, ClientSize.Height - LblInfo.Height - BtnAll.Height - 30)
        BtnAll.Location = New Point(10, TabControl.Bottom + 5)
        BtnReverse.Location = New Point(BtnAll.Right + 5, TabControl.Bottom + 5)
        BtnCancel.Location = New Point(BtnReverse.Right + 5, TabControl.Bottom + 5)
        BtnAdd.Location = New Point(BtnCancel.Right + 5, TabControl.Bottom + 5)
        BtnAdd.Size = New Size(TabControl.Right - BtnAdd.Left, BtnAdd.Height)
    End Sub

    Private Sub BtnAll_Click(sender As Object, e As EventArgs) Handles BtnAll.Click
        Dim ListView As ListView = New ListView
        Dim i As Integer
        If TabControl.SelectedIndex = 0 Then
            ListView = ListView1
        ElseIf TabControl.SelectedIndex = 1 Then
            ListView = ListView2
        ElseIf TabControl.SelectedIndex = 2 Then
            ListView = ListView3
        End If
        For i = 0 To ListView.Items.Count - 1
            ListView.Items(i).Checked = True
        Next
    End Sub

    Private Sub BtnReverse_Click(sender As Object, e As EventArgs) Handles BtnReverse.Click
        Dim ListView As ListView = New ListView
        If TabControl.SelectedIndex = 0 Then
            ListView = ListView1
        ElseIf TabControl.SelectedIndex = 1 Then
            ListView = ListView2
        ElseIf TabControl.SelectedIndex = 2 Then
            ListView = ListView3
        End If
        For i = 0 To ListView.Items.Count - 1
            ListView.Items(i).Checked = Not ListView.Items(i).Checked
        Next
    End Sub

    Private Sub BtnCancel_Click(sender As Object, e As EventArgs) Handles BtnCancel.Click
        Dim ListView As ListView = New ListView
        If TabControl.SelectedIndex = 0 Then
            ListView = ListView1
        ElseIf TabControl.SelectedIndex = 1 Then
            ListView = ListView2
        ElseIf TabControl.SelectedIndex = 2 Then
            ListView = ListView3
        End If
        For i = 0 To ListView.Items.Count - 1
            ListView.Items(i).Checked = False
        Next
    End Sub

    Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
        Dim ListView As ListView = New ListView
        Dim i As Integer
        Dim j As Integer
        Dim s As String = ""
        For i = 0 To 2
            If i = 0 Then
                ListView = ListView1
                s = "app"
            ElseIf i = 1 Then
                ListView = ListView2
                s = "sys"
            ElseIf i = 2 Then
                ListView = ListView3
                s = "local"
            End If
            If ListView.Items.Count = 0 Then Continue For
            For j = 0 To ListView.Items.Count - 1
                If ListView.Items(j).Checked = True Then
                    If ItemExists(JsonPre(s)(j)("command").ToString) = False Then
                        FrmSetting.JsonSetting.Add(JsonPre(s)(j))
                        FrmSetting.JsonBackup.Add(JsonPre(s)(j))
                        ReDim Preserve FrmSetting.IconFile(FrmSetting.JsonSetting.Count - 1)
                        FrmSetting.ListView.Items.Add(JsonPre(s)(j)("name").ToString)
                    End If
                Else
                    If ItemExists(JsonPre(s)(j)("command").ToString) Then
                        Dim k As Integer
                        For k = FrmSetting.JsonSetting.Count - 1 To 0 Step -1
                            If FrmSetting.JsonSetting(k)("command").ToString = JsonPre(s)(j)("command").ToString Then
                                FrmSetting.JsonSetting(k).Remove()
                                FrmSetting.JsonBackup(k).Remove()
                                FrmSetting.IconFile = RemoveAt(FrmSetting.IconFile, k)
                                FrmSetting.ListView.Items(k).Remove()
                            End If
                        Next
                    End If
                End If
            Next
        Next
        If FrmSetting.JsonSetting.Count > 0 Then
            FrmSetting.ListView.Items(0).Selected = True
            FrmSetting.ListView.Items(0).EnsureVisible()
        Else
            FrmSetting.LastSelect = -1
        End If
        Close()
    End Sub

    Private Function ItemExists(strCmd As String) As Boolean
        Dim i As Integer
        If FrmSetting.JsonSetting.Count = 0 Then Return False
        For i = 0 To FrmSetting.JsonSetting.Count - 1
            If FrmSetting.JsonSetting(i)("command").ToString = strCmd Then
                Return True
            End If
        Next
        Return False
    End Function
End Class
